<?php

namespace app\common\library\xiluhouse;

use think\Config;
use JPush\Client;
use JPush\Exceptions\JPushException;
use think\Db;

class Jpush
{
    private $client = null;

    /**
     * 错误内容
     */
    protected $_error = '';

    public function __construct()
    {
        $config = get_addon_config('xiluhouse');
        $this->client = new Client($config['JpushAppKey'], $config['JpushSecret']);
    }


    /**
     * 获取最后产生的错误
     * @return string
     */
    public function getError()
    {
        return $this->_error;
    }

    /**
     * 设置错误
     * @param string $error 信息信息
     */
    protected function setError($error)
    {
        $this->_error = $error;
    }

    /**
     * 发送消息
     * @param [string] $[content]    [发送内容]
     * @param [string] $[platform]   [推送平台设置] 【all、android、ios】
     * @param [string] $[audience_type]  [推送目标] 【all、tag、alias、registration_id】
     * @param [array] $[extras]                   [扩展字段]   
     * @param [array] $[alias]                   [别名]   
     * @param [array] $[tag]                     [标签]   
     * @param [array] $[registration_id]         [设备]   
     * @return boolean
     */
    public function send($content, $platform, $audience_type, $extras = [], $tag = [], $alias = [], $registration_id = [])
    {
        $pusher = $this->client->push();
        $result = false;

        if (!$content) {
            $this->setError('发送内容不能为空');
        }
        if (!$platform) {
            $this->setError('发送平台不能为空');
        }
        if (!$audience_type) {
            $this->setError('推送目标不能为空');
        }
        $pusher->setNotificationAlert($content);
        $pusher->iosNotification($content, array(
            'extras' => $extras,
        ));
        $pusher->androidNotification($content, array(
            'extras' => $extras,
        ));
        $pusher->setPlatform($platform);

        switch ($audience_type) {
            case 'all':
                $pusher->addAllAudience();
                break;
            case 'tag':
                if (!$tag) {
                    $this->setError('标签不能为空');
                }
                $pusher->addTagAnd($tag);

                // foreach ($tag as $k => $value) {
                //     $pusher->addTag($value);
                //     // $pusher->addTagAnd($value);
                // }
                break;
            case 'alias':
                $pusher->addAlias(implode(',', $alias));
                break;
            case 'registration_id':
                $pusher->addRegistrationId(implode(',', $registration_id));
            default:
                # code...
                break;
        }
        try {
            $res = $pusher->send();
            if (isset($res['http_code']) && $res['http_code'] == 200) {
                $result = true;
            } else {
                $this->setError('发送失败，code：' . ': ' . $res['http_code']);
            }
        } catch (JPushException $e) {
            $this->setError('发送失败，原因：' . ': ' . $e->getMessage());
        }
        return true;
    }
}
